package com.qst.rongxiaotong.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.rongxiaotong.mapper.QuestionMapper;
import com.qst.rongxiaotong.pojo.Question;
import com.qst.rongxiaotong.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    QuestionMapper questionMapper;

    private Integer pageSize = 30;


    //增加问答
    @Override
    public void insert(Question question) {
        //UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //String username = principal.getUsername();
        //question.setQuestioner(username);
        question.setQuestioner("wyn3");
        questionMapper.insertSelective(question);
    }

    //根据id查询问答
    @Override
    public Question selectById(Integer id) {
        Question question = questionMapper.selectByPrimaryKey(id);
        return question;
    }

    //查询所有问答信息
    @Override
    public PageInfo<Question> findPage(Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<Question> experts = questionMapper.selectAllQuestion();
        PageInfo<Question> expertPageInfo = new PageInfo<>(experts);
        return expertPageInfo;
    }


    //分页条件查询问答
    @Override
    public PageInfo<Question> selectByKeys(String keys, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<Question> questions = questionMapper.selectAllByKeys(keys);
        PageInfo<Question> questionPageInfo = new PageInfo<>(questions);
        return questionPageInfo;
    }

    //根据用户查询询问情况
    @Override
    public List<Question> selectByQuestion(String type) {
        Question question = new Question();
        question.setExpertName("gaoge");
        //UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //String username = principal.getUsername();
        //if ("questioner".equals(type)){
        //    question.setQuestioner(username);
        //}else {
        //    question.setExpertName(username);
        //}
        List<Question> questions = questionMapper.selectByQuestion(question);
        return questions;
    }

    //根据Id修改问答
    @Override
    public void updateById(Question question) {
        questionMapper.updateByPrimaryKeySelective(question);
    }

    //根据Id删除问答
    @Override
    public void delete(Integer id) {
        questionMapper.deleteByPrimaryKey(id);
    }
}
